package dao.Impl;

import dao.EmployeeChangeDao;
import entity.EmployeeChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmployeeChangeDaoImp implements EmployeeChangeDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 创建员工变更记录
    public Boolean create(EmployeeChange employeeChange) {
        String sql = "INSERT INTO employee_change (emp_id, create_user_id, review_status, review_user_id) VALUES (?, ?, ?, ?)";
       return jdbcTemplate.update(sql,
                employeeChange.getEmpId(),
                employeeChange.getCreateUserId(),
                employeeChange.getReviewStatus(),
                employeeChange.getReviewUserId())>0;
    }

    // 查询所有员工变更记录
    public List<EmployeeChange> findAll() {
        String sql = "SELECT * FROM employee_change";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(EmployeeChange.class));
    }

    // 根据ID查询单个员工变更记录
    public EmployeeChange findById(Long id) {
        String sql = "SELECT * FROM employee_change WHERE emp_id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, BeanPropertyRowMapper.newInstance(EmployeeChange.class));
    }

    // 更新员工变更记录
    public Boolean update(EmployeeChange employeeChange) {
        String sql = "UPDATE employee_change SET  create_user_id=?, review_status=?, review_user_id=? WHERE emp_id=?";
        return  jdbcTemplate.update(sql,
                employeeChange.getCreateUserId(),
                employeeChange.getReviewStatus(),
                employeeChange.getReviewUserId(),
                employeeChange.getEmpId())>0;
    }

    // 删除员工变更记录
    public Boolean deleteById(Long id) {
        String sql = "DELETE FROM employee_change WHERE emp_id = ?";
      return   jdbcTemplate.update(sql, id)>0;
    }


}